# from concurrent.futures import ProcessPoolExecutor
# import os
# import time
# import random
# def func(a,b):
#     print(os.getpid(),'start',a,b)
#     time.sleep(random.randint(1,4))
#     print(os.getpid(), 'end', a, b)
#
# if __name__ == '__main__':
#     tp=ProcessPoolExecutor(4)
#     for i in range(10):
#         tp.submit(func,i,i+1)

#获取任务结果
# from concurrent.futures import ProcessPoolExecutor
# import os
# import time
# import random
# def func(a,b):
#     print(os.getpid(),'start',a,b)
#     time.sleep(random.randint(1,4))
#     print(os.getpid(), 'end', a, b)
#     return a*b
#
# if __name__ == '__main__':
#     tp=ProcessPoolExecutor(4)
#     future_l=[]
#     for i in range(10):#异步非阻塞
#         ret=tp.submit(func,i,i+1)
#         # print(ret)#<Future at 0x1f1430049e8 state=running> future 未来对象
#         # print(ret.result())#ret.result()是程序同步了
#         future_l.append(ret)
#     for obj in future_l:#同步阻塞的,按照顺序获取结果
#         print(obj.result())

#map对象版本
from concurrent.futures import ProcessPoolExecutor
import os
import time
import random
def func(a):
    b=a+1
    print(os.getpid(),'start',a,b)
    time.sleep(random.randint(1,4))
    print(os.getpid(), 'end', a, b)
    return a*b

if __name__ == '__main__':
    tp=ProcessPoolExecutor(4)
    ret=tp.map(func,range(20))
    for obj in ret:#同步阻塞的,按照顺序获取结果
        print(obj)













